<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout()->setLayout('layout');
    }

    public function indexAction()
    {
    	$index = $this->view;
    	$this->view->setEscape('htmlentities');
    	
    	$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
			
			$this->view->identity = $auth->getIdentity();
			$this->view->loggedUser = $auth->getIdentity();
			$username = $auth->getIdentity();
			if ($username != null){
				
				
			}
		}
    }

	public function whatsAction()
    {
		
    }
    
    public function downloadAction()
    {
        // action body
    }
    
    public function faqAction()
    {
        // action body
    }
    
	public function registerAction()
    {
    	
		$form = new Application_Form_Registration();
		$index = $this->view;
		$index->form = $form;
    	
    }
	public function loginAction()
    {
		$login = $this->view;
		if($this->_request->isPost()){
		
		
			$username = $this->_request->getParam('username');
			$password = $this->_request->getParam('password');

			$dbAdapter = Zend_Db_Table::getDefaultAdapter();
			$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter,'auth_table','username','password');
			$authAdapter->setIdentity($username);
			$authAdapter->setCredential(md5($password));
			
			
			$result = $authAdapter->authenticate();
			if($result->isValid()){
			
				$auth = Zend_Auth::getInstance();
				$storage = $auth->getStorage();
				$storage->write($authAdapter->getResultRowObject(array('username','password',)));
				$login->username = $username;
				$login->islogged = true;
				$contentstable = new Application_Model_DbTable_Contents();
				$login->contents = $contentstable->getAllContents();
				
				// fetch the bug paginator adapter
		        
		        $adapter = $contentstable->fetchAll();
		        $paginator = new Zend_Paginator($adapter);
		        // show 10 bugs per page
		        $paginator->setItemCountPerPage(10);
		        // get the page number that is passed in the request. 
		        //if none is set then default to page 1.
		        $page = $this->_request->getParam('page', 1);
		        $paginator->setCurrentPageNumber($page);
		        // pass the paginator to the view to render 
		        $this->view->paginator = $paginator;
			}else{
					
				$login->loginMessage = "Sorry, your username or password was incorrect";
			} 

		
		}
    }
    	
    public function logoutAction(){
    	$logout = $this->view;
    	$authAdapter = Zend_Auth::getInstance();
    	$authAdapter->clearIdentity();
    	$logout->islogged = false;
    	
    }
	public function allcontentsAction(){
    	$logout = $this->view;
    	$authAdapter = Zend_Auth::getInstance();
    	$authAdapter->clearIdentity();
    	$logout->islogged = false;
    	
    }
public function mycontentsAction(){
		
    	$contentview = $this->view;
    	$authAdapter = Zend_Auth::getInstance();
    	if($authAdapter->hasIdentity()){
    		$username = $this->_request->getParam('username');
    		$contentstable = new Application_Model_DbTable_Contents();
				$login->contents = $contentstable->getAllContents();
				
				// fetch the bug paginator adapter
			    
				if ($username != null){
						$db = Zend_Db::factory('Pdo_Mysql', array(
					    'host'             => 'localhost',
					    'username'         => 'vidalelu_epic',
					    'password'         => 'vidale509123',
					    'dbname'           => 'vidalelu_epic'
					));
					
					$sql = 'SELECT * FROM '. $username;
					$adapter = $db->fetchAll($sql);
					
					//imposta la variabile per il paginator
					$contentview->contents = $adapter;
					
			        $paginator = new Zend_Paginator($adapter);
			        // show 10 contents per page
			        $paginator->setItemCountPerPage(10);
			        // get the page number that is passed in the request. 
			        //if none is set then default to page 1.
			        $page = $this->_request->getParam('page', 1);
			        $paginator->setCurrentPageNumber($page);
			        // pass the paginator to the view to render 
			        $this->view->paginator = $paginator;
    	}else{
    		$this->_forward("index");
    	}
    		
    	}
	}
}

